<!--<link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">-->
<link rel="stylesheet" href="lib/jq-module/zyupload/zyupload-1.0.0.min.css" media="all">
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote">
            这里是一个系统通知，可在后台进行设定
        </blockquote>
        <form class="layui-form" action="" lay-filter="albumEdit">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>相册信息</legend>
            </fieldset>
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入相册标题"
                           class="layui-input" id="album-title-text">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">简介</label>
                <div class="layui-input-block">
                    <textarea name="describe" autocomplete="off" placeholder="请输入相册简介" class="layui-input" id="album-des-text"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否公开</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="isPublic" lay-skin="switch" lay-text="是|否">
                </div>
            </div>
            <center>
                <button class="layui-btn number-of-people-btn" lay-submit="" lay-filter="save">保存</button>
            </center>
        </form>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>相册资源表</legend>
        </fieldset>
        <script type="text/html" id="SchooltoolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除所选资源 </button>
            </div>
        </script>
        <table class="layui-hide" id="albumTableId" lay-filter="albumTableFilter"></table>
        <script type="text/html" id="albumTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">管理资源</a>
        </script>



    </div>
</div>
<script src="lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script type="text/javascript">
    layui.use(['form', 'layedit', 'laydate', 'table', 'miniPage', 'element'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , table = layui.table
            , miniPage = layui.miniPage;

        //数据初始化
        var schoolAlbumId=localStorage.getItem('selectedSchoolAlbumId');
        jq1111.ajax({
            type: "GET",
            crossDomain:true,
            url: BasePath+"api/SchoolAlbum/"+schoolAlbumId+"/Detail",
            contentType: "application/json",
            dataType:"json",
            xhrFields: {
                withCredentials: true
            },
            success:function (message) {
                console.log(message);
                jq1111("#album-des-text").val(message.data.title);
                jq1111("#album-title-text").val(message.data.description);
                form.val("albumEdit", {
                    "isPublic": message.data.isPublic
                });
            },
            error:function (message) {
                console.log(message.responseJSON);
            },
        });
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //监听提交
        form.on('submit(save)', function (data) {
            var isPub=true;
            if(data.field.isPublic==='off'){
                isPub=false;
            }
            jq1111.ajax({
                type: "POST",
                crossDomain:true,
                url: BasePath+"api/SchoolAlbum/"+schoolAlbumId+"/Update",
                contentType: "application/json",
                dataType:"json",
                xhrFields: {
                    withCredentials: true
                },
                data:JSON.stringify({
                    "title": data.field.title,
                    "description": data.field.describe,
                    "isPublic": isPub
                }),
                success:function (message) {
                    console.log(message);
                    layer.alert(message.msg, {
                        title: '操作结果'
                    })
                    location.reload()
                },
                error:function (message) {
                    console.log(message.responseJSON);
                    layer.alert(message.responseJSON.msg, {
                        title: '操作结果'
                    })
                },
            });

            return false;
        });

        //相册表相关js
        table.render({
            elem: '#albumTableId',
            url:BasePath+'api/Album/'+schoolAlbumId+'/Resources',
            toolbar: '#SchooltoolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {title: '操作', minWidth: 100, toolbar: '#albumTableBar', align: "center"},
                {field: 'resourceTitle', width: 80, title: '资源标题'},
                {field: 'resourceDescription', width: 280, title: '资源描述'},
                {field: 'shootingDate', title: '拍摄时间', minWidth: 150},
                {field: 'location', title: '拍摄地点', minWidth: 150},
                {field: 'downloadCount', width: 135, title: '点击量', sort: true}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        //toolbar事件监听
        table.on('toolbar(albumTableFilter)', function (obj) {
            if (obj.event === 'delete') {   // 监听添加操作
                var checkStatus = table.checkStatus('albumTableId')
                    , data = checkStatus.data;
                if (data.length === 0) {
                    layer.alert('未选择要删除的数据', {
                        title: '操作不合法'
                    });
                    return;
                }
                var deletedItemCount = 0;
                data.forEach(function(deleteData) {
                    if (deleteRes(deleteData.resourceId)) {
                        deletedItemCount++;
                    }
                });
                layer.alert('成功删除' + deletedItemCount + '个数据,' + (data.length - deletedItemCount) + '条删除失败，如果存在删除失败，请尝试刷新界面重试', {
                    title: '操作结果'
                });
                table.reload('albumTableId', {}, 'data');

            }
        });

        // 操作点击事件
        table.on('tool(albumTableFilter)', function(obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                console.log(data)
                var content = miniPage.getHrefContent('page/admin/school-album-manage/resEdit.html');
                var openWH = miniPage.getOpenWidthHeight();
                localStorage.setItem('selectedResId',data.resourceId);
                // localStorage.setItem('selectedResType',data.resourceType);


                var index = layer.open({
                    title: '编辑资源',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function() {
                    layer.full(index);
                });
                return false;
            }
        });

        //删除资源函数
        function deleteRes(resId){
            var result=false;
            jq1111.ajax({
                url:BasePath+"api/Resource/"+resId+"/Delete",
                async:false,
                type:"POST",
                xhrFields: {
                    withCredentials: true
                },
                success:function (message) {
                    result=true;
                },
                error:function (message) {
                    result=false;
                    console.log(message.responseJSON);
                }
            });
            return result;
        }

    });
</script>